<?
require_once('version.inc');
require_once('dbaccess.inc');
require_once('language.inc');
require_once('header.inc');
require_once('crypt.inc');
require_once('settings.inc');
require_once('menu.inc');

function SaveNewPhase() {
	$kp_num = $_REQUEST['kp_num'];
	$kp_phase = $_REQUEST['kp_phase'];

	$db = OpenDB();
	$query = "select kp_phase from key_phase where kp_num = $kp_num";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);

	if ($numrows == 1) {
	   Error("Schl&uuml;ssel $kp_num ist bereits vorhanden!\n");
	   closeDB($db);
	   return false;
	}

	$query = "insert into key_phase (kp_num, kp_phase) values ($kp_num, '$kp_phase')";

	if (!TQueryDB($db, $query)) {
	   Error("SQL-Error: $query");
	   closeDB($db);
	   return false;
	}

	closeDB($db);
	return true;
}

function SaveEditPhase() {
	$kp_num = $_REQUEST['kp_num'];
	$kp_phase = $_REQUEST['kp_phase'];

	$db = OpenDB();
	$query = "select kp_phase from key_phase where kp_num = $kp_num";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);

	if ($numrows != 1) {
	   Error("Schl&uuml;ssel $kp_num ist nicht vorhanden!\n");
	   closeDB($db);
	   return false;
	}

	$query = "update key_phase set kp_phase = '$kp_phase' where kp_num = $kp_num";

	if (!TQueryDB($db, $query)) {
	   Error("SQL-Error: $query");
	   closeDB($db);
	   return false;
	}

	closeDB($db);
	return true;
}

$newphase = $_REQUEST['newphase'];
$editphase = $_REQUEST['editphase'];

if (!isset($editphase)) {
   $editphase = 0;
}

$savenewphase = $_REQUEST['savenewphase'];
$saveeditphase = $_REQUEST['saveeditphase'];

if (isset($savenewphase)) {
   SaveNewPhase();
}

if (isset($saveeditphase)) {
   SaveEditPhase();
}

?>
<form action="phase.php" method="post">
<input type="hidden" name="headline" value="15">
<input type="hidden" name="rstufe" value="<? echo "$rstufe"; ?>">
<input type="hidden" name="menu" value="<? echo "$menu"; ?>">
<input type="hidden" name="unum" value="<? echo "$unum"; ?>">
<button type="submit" name="newphase" value="newphase"><img src="image/button_newphase.png" border=0 alt="Neue Phase"></button>
<?php
$db = OpenDB();
$query = "select kp_num, kp_phase from key_phase order by kp_num";
$result = QueryDB($db, $query);

if (!$result) {
   echo "</table></form><br>\n";
   Error("SQL-Error: $query");
   require('footer.inc');
   exit;
}

$numrows = numrowsDB($result);

if ($numrows > 0 || isset($newphase)) {
?>
<table border=1>
   <tr>
      <td class="tbhead">&nbsp;</td>
      <td class="tbhead">Key</td>
      <td class="tbhead">Text</td>
   </tr>
<?php
   $row = 0;

   while ($row < $numrows) {
      $data = fetchDB($result, $row);
      $kp_num = $data[0];
      $kp_phase = $data[1];

      if ($kp_num == $editphase) {
         echo "<input type=\"hidden\" name=\"kp_num\" value=\"$kp_num\">\n";
         echo "<tr><td><button type=\"submit\" name=\"saveeditphase\" value=\"$kp_num\"><img src=\"image/new.png\" border=0 alt=\"Edit\"></button></td>";
         echo "<td>$kp_num</td>";
         echo "<td><input type=\"text\" name=\"kp_phase\" value=\"$kp_phase\" size=40 maxlength=60></td></tr>\n";
      } else {
         echo "<tr><td><button type=\"submit\" name=\"editphase\" value=\"$kp_num\"><img src=\"image/edit.png\" border=0 alt=\"Edit\"></button></td>";
         echo "<td>$kp_num</td><td>$kp_phase</td></tr>";
      }

      $row++;
   }

   if (isset($newphase)) {
      echo "<tr><td><button type=\"submit\" name=\"savenewphase\" value=\"savenewphase\"><img src=\"image/new.png\" border=0 alt=\"Neu\"></button></td>";
      echo "<td><input type=\"text\" name=\"kp_num\" size=7 maxlength=7></td>";
      echo "<td><input type=\"text\" name=\"kp_phase\" size=40 maxlength=60></td></tr>\n";
   }
?>
</table>
<button type="submit" name="newphase" value="new"><img src="image/button_newphase.png" border=0 alt="Neue Phase"></button>
</form>
<?php
}

require('footer.inc');
?>
